package com.huitone.gddw.service.impl.system;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huitone.gddw.dao.entity.Dept;
import com.huitone.gddw.dao.system.IDeptDao;
import com.huitone.gddw.model.support.QueryPage;
import com.huitone.gddw.service.dto.system.DeptDTO;
import com.huitone.gddw.service.system.IDeptService;
import com.huitone.gddw.service.system.IDeptSrvNodeService;
import com.huitone.gddw.utils.SysLoginUtil;

/**
 * 组织单位管理 接口实现类
 * @author wuluodan
 *
 */
@Service
public class DeptService implements IDeptService {

	@Autowired
	private IDeptDao deptDao;
	
	@Autowired
	private IDeptSrvNodeService deptSrvNodeService;
	
	@Override
	public QueryPage<DeptDTO> selectPage(QueryPage<Dept> page) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("start", (page.getPageNo() - 1) * page.getPageSize() + 1);
		map.put("end", page.getPageNo() * page.getPageSize());
		List<DeptDTO> result = deptDao.selectPage(map);
		int totalCount = deptDao.selectCount(map);
		
		QueryPage<DeptDTO> queryPage = new QueryPage<DeptDTO>();
		queryPage.setResult(result);
		queryPage.setPageNo(page.getPageNo());
		queryPage.setPageSize(page.getPageSize());
		queryPage.setTotalCount(totalCount);
		return queryPage;
	}
	
	@Override
	public Dept getById(String deptId) {
		return deptDao.getById(deptId);
	}
	
	@Override
	public List<Dept> getAll() {
		return deptDao.getAll();
	}

	@Override
	public List<Dept> getByParentId(String parentId) {
		return deptDao.getByParentId(parentId);
	}
	
	@Override
	public List<Dept> getUpLevelDept(String deptLevel) {
		return deptDao.getDeptByLevel(String.valueOf(Integer.parseInt(deptLevel) - 1));
	}

	@Override
	public DeptDTO toUpdate(String deptId) {
		return deptDao.toUpdate(deptId);
	}

	@Override
	public void save(DeptDTO deptDTO) {
		Dept dept = new Dept();
		BeanUtils.copyProperties(deptDTO, dept);
		dept.setDeptId(deptDao.getSequence());
		this.save(dept);
	}
	
	private void save(Dept entity) {
		entity.setInsertTime(new Date());
		entity.setInsertStaffId(SysLoginUtil.getLoginUserId());
		entity.setLastModTime(entity.getInsertTime());
		entity.setLastModStaffId(entity.getInsertStaffId());
		deptDao.insertSelective(entity);
	}

	@Override
	public void update(DeptDTO deptDTO) {
		Dept dept = this.getById(deptDTO.getDeptId());
		dept.setDeptName(deptDTO.getDeptName());
		dept.setDeptLevel(deptDTO.getDeptLevel());
		dept.setParentId(deptDTO.getParentId());
		dept.setRemark(deptDTO.getRemark());
		this.update(dept);
	}
	
	private void update(Dept entity) {
		entity.setLastModTime(new Date());
		entity.setLastModStaffId(SysLoginUtil.getLoginUserId());
		deptDao.updateByPrimaryKey(entity);
	}

	@Override
	public void delete(String deptId) {
		deptSrvNodeService.delAllByDeptId(deptId);
		deptDao.deleteByPrimaryKey(deptId);
	}

	@Override
	public void changeDeptSvrNodes(String deptId, String svrNodes) {
		deptSrvNodeService.changeDeptSvrNodes(deptId, svrNodes);
	}

	@Override
	public List<Dept> getByName(String deptName) {
		return deptDao.getByName(deptName);
	}

}
